﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using AspNetCs.Common;
using AspNetCs.Bll;
using AspNetCs.Enumerations.TrueOrFalseEnum;
using System.Text.RegularExpressions;
using AspNetCs.Enumerations.User_Enum;

public partial class Master_AdminGuest : Admin_BasePage
{
    private GuestBLL guestBLL = new GuestBLL();
    protected string Action = "";
    protected string WhereStr = " Where GuestId<>0 ";
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check
        if (Request["Action"] != null)
        {
            Action = StringHelper.DelSQLStr(Request["Action"]);
        }
        if (Action == "Check")
        {
            WhereStr += " and  IsPassed=" + (int)IsTrueEnum.NO;
        }
        IsExit();
        if (!IsPostBack)
        {
            ViewState["Action"] = Request["Action"];
            ViewState["GuestId"] = Request["GuestId"];
            initPage();
        }
    }

    protected void initPage()
    {
        string Action = "";
        string GuestId = "0";
        if (ViewState["Action"] != null)
        {
            Action = ViewState["Action"].ToString();
        }
        if (ViewState["GuestId"] != null)
        {
            GuestId = ViewState["GuestId"].ToString();
        }
        switch (Action)
        {
            case "Reply":
                dataBindGuest(Convert.ToInt32(GuestId));
                palReply.Visible = true;
                palGuestList.Visible = false;
                break;
            default:
                dataBindGuestList();
                palReply.Visible = false;
                palGuestList.Visible = true;
                break;
        }
    }

    /// <summary>
    /// 绑定用户留言的详细信息
    /// </summary>
    protected void dataBindGuest(int guestId)
    {
        DataTable dataTable = guestBLL.getGuestInfoByGuestId(guestId);
        rptGuest.DataSource = dataTable;
        rptGuest.DataBind();
    }

    //绑定留言内容
    protected void dataBindGuestList()
    {
        DataPagesOperate pager = new DataPagesOperate();
        string cmdText = "select * from Guest " + WhereStr + " order by GuestTime desc";
        pager.BindRepeater(cmdText, AspNetPager1, "GuestTime desc", "Guestid", rptGuestList);
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        dataBindGuestList();
    }
    protected string GuestUserName(object s, object ip)
    {
        Regex x = new Regex(@"(?<one>([0-9]{1,3}))\.(?<Two>([0-9]{1,3}))\.(?<three>([0-9]{1,3}))\.(?<Four>([0-9]{1,3}))");
        Match mc = x.Match(ip.ToString());

        string Result = "";
        try
        {
            if (s.ToString() == "")
            {
                Result = "【游客】<br>" + mc.Groups["one"].Value + ".*.*" + mc.Groups["Four"].Value;
            }
            else
            {
                Result = s.ToString();
            }
        }
        catch
        {
            Result = "";
        }
        return Result;
    }
    protected string UserImg(object num)
    {
        string Result = "";
        if (StringHelper.isNumber_int(num) == (int)Sex_Enum.boy)
        {
            Result = "<img src='" + configInfo.InstallDir + "images/01.gif' alt='男' width='80' height='90' >";

        }
        else if (StringHelper.isNumber_int(num) == (int)Sex_Enum.gril)
        {
            Result = "<img src='" + configInfo.InstallDir + "images/18.gif' alt='女'  width='80' height='90'>";
        }
        else
        {
            Result = "<img src='" + configInfo.InstallDir + "images/NoheaderImg.gif' alt='未知'  width='80' height='90'>";
        }



        return Result;
    }
    protected void rptGuestList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string commandName = e.CommandName;
        HiddenField hldGuestId = e.Item.FindControl("hldGuestId") as HiddenField;
        HiddenField hldIsPassed = e.Item.FindControl("hldIsPassed") as HiddenField;
        if (hldGuestId != null && hldIsPassed != null)
        {
            string GuestId = hldGuestId.Value;
            string isPassed = hldIsPassed.Value;
            if (commandName == "Delete")
            {
                guestBLL.deleteGuestByGuestId(Convert.ToInt32(GuestId));
            }
            else if (commandName == "Check")
            {
                int ispass = Convert.ToInt32(isPassed);
                ispass = (ispass == 0 ? 1 : 0);
                guestBLL.checkPass(Convert.ToInt32(GuestId), ispass);
            }
        }
        Repeater rpt = source as Repeater;
        if (rpt != null)
        {
            if (rpt.ID == "rptGuestList")
            {
                ViewState["Action"] = null;
            }
            else if (rpt.ID == "rptGuest")
            {
                ViewState["Action"] = "Reply";
            }
            if (commandName == "Delete")
            {
                ViewState["Action"] = null;
            }
            initPage();
        }


    }

    protected void btnPublish_Click(object sender, EventArgs e)
    {
        string adminReply = txtGuestContent.Text;
        string GuestId = "0";
        if (ViewState["GuestId"] != null)
        {
            GuestId = ViewState["GuestId"].ToString();
            int count = 0;
            count = guestBLL.addReply(Convert.ToInt32(GuestId), adminReply, DateTime.Now);
            if (count > 0)
            {
                showMessage("publishSuccee", "<script>alert('回复成功');</script>");
            }
            else
            {
                showMessage("publishFail", "<script>alert('回复失败');</script>");
            }
            ViewState["Action"] = "Reply";
            initPage();
        }

    }

    protected void btnReset_Click(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 通过审核或取消审核
    /// </summary>
    protected void CheckOrCancleCheck(string type)
    {
        int isPassed = 0;
        if (type == "Check")
        {
            isPassed = 1;
        }
        else if (type == "CancleCheck")
        {
            isPassed = 0;
        }
        for (int i = 0; i < rptGuestList.Items.Count; i++)
        {
            CheckBox chkSel = rptGuestList.Items[i].FindControl("chkSel") as CheckBox;
            HiddenField hldGuestId = rptGuestList.Items[i].FindControl("hldGuestId") as HiddenField;
            if (chkSel != null && hldGuestId != null)
            {
                if (chkSel.Checked)
                {
                    int GuestId = Convert.ToInt32(hldGuestId.Value);
                    guestBLL.checkPass(GuestId, isPassed);
                }

            }
        }
        ViewState["Action"] = null;
        initPage();
    }

    protected void chkAll_OnCheckedChanged(Object sender, EventArgs e)
    {
        for (int i = 0; i < rptGuestList.Items.Count; i++)
        {
            CheckBox chkSel = rptGuestList.Items[i].FindControl("chkSel") as CheckBox;
            if (chkSel != null)
            {
                chkSel.Checked = chkAll.Checked;
            }
        }
    }

    protected void btnDeleteGuest_Click(Object sender, EventArgs e)
    {
        DeleteGuest();
    }

    protected void btnCheck_Click(Object sender, EventArgs e)
    {
        CheckOrCancleCheck("Check");
    }

    protected void btnCancleCheck_Click(Object sender, EventArgs e)
    {
        CheckOrCancleCheck("CancleCheck");
    }

    /// <summary>
    /// 删除选中的留言
    /// </summary>
    protected void DeleteGuest()
    {
        for (int i = 0; i < rptGuestList.Items.Count; i++)
        {
            if (rptGuestList.Items[i].ItemType == ListItemType.Item || rptGuestList.Items[i].ItemType == ListItemType.AlternatingItem)
            {
                CheckBox chkSel = rptGuestList.Items[i].FindControl("chkSel") as CheckBox;
                HiddenField hldGuestId = rptGuestList.Items[i].FindControl("hldGuestId") as HiddenField;
                if (chkSel != null && hldGuestId != null)
                {
                    if (chkSel.Checked)
                    {
                        int GuestId = Convert.ToInt32(hldGuestId.Value);
                        guestBLL.deleteGuestByGuestId(GuestId);
                    }

                }
            }
        }
        ViewState["Action"] = null;
        initPage();
    }
}
